import { defineStore } from 'pinia'
import { login, logout, getInfo,getCljjList } from '@/api/user'

const TokenKey = 'DataV-Token'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    name: '',
    avatar: '',
    role: -1,
  }),
  actions: {
    async login(username: string, password: string) {
      // try {
        const res = await login(username, password)
        // if (res.data.code === 0) {
          this.token ="[Vzht6Llz20I4(6fBt8fZuBZ6)6)lt"
          localStorage.setItem(TokenKey, "[Vzht6Llz20I4(6fBt8fZuBZ6)6)lt")
      //   } else {
      //     throw Error(res.data.message)
      //   }
      // } catch (error) {
      //   throw error
      // }
    },
    async getUserInfo() {
      try {
          // const res = await getInfo();
          this.name = "美人骨"
          this.avatar = "http://files.pengxiaotian.com/avatar.jpeg"
          this.role = 1
       
      } catch (error) {
        throw error
      }
    },
    resetToken() {
      this.token = ''
      localStorage.removeItem(TokenKey)
    },
    isLogin() {
      // const token = localStorage.getItem(TokenKey)
      // return !!token
      return true;
    },
    async logout() {
      await logout()
      this.resetToken()
    },
    async getCljjList(pageNO: string, pageSize: string) {
      const res = await getCljjList(pageNO, pageSize)
      return res.data;
    },
  },
})
